package com.msk.web.controller.wlhy;

import com.msk.common.core.controller.BaseController;
import com.msk.common.core.domain.R;
import com.msk.common.core.domain.model.LoginUser;
import com.msk.common.utils.StringUtils;
import com.msk.framework.web.utils.TokenUtils;
import com.msk.wlhy.domain.Complain;
import com.msk.wlhy.service.ComplainService;
import com.msk.wlhy.service.InformationService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author llh
 * @date 2023/5/19
 * @description 咨询投诉接口
 */
@Api(tags = "咨询投诉接口")
@RestController
@RequestMapping("/api/consultationAndComplaints")
public class ConsultationAndComplaintsController extends BaseController {

    @Autowired
    private ComplainService complainService;

    @Autowired
    private InformationService informationService;

    /**
     * @author llh
     * @date 2023/5/19
     * @return com.msk.common.core.domain.R<?>
     * @description 获取咨询投诉列表接口
     */
    @ApiOperation("获取咨询投诉列表接口")
    @PostMapping("/getComplainList")
    public R<?> getComplainList(@RequestBody Complain complain) {
        //获取用户信息
        LoginUser loginUser = TokenUtils.getMpLoginUser();
        complain.setUserId(loginUser.getLoginId());
        //分页
        complain.getParams().put("orderByStr", "a.create_date desc");
        startPageJson(complain.getParams(), "");
        List<Complain> list = complainService.findList(complain);
        informationService.updateInformation(complain.getUserId());
        for (Complain cp : list) {
            String complainContent = URLDecoder.decode(cp.getComplainContent());
            cp.setComplainContent(complainContent);
        }
        return R.appOk(getDataTable(list));
    }

    /**
     * @author llh
     * @date 2023/5/19
     * @return com.msk.common.core.domain.R<?>
     * @description 新增咨询投诉接口
     */
    @ApiOperation("新增咨询投诉接口")
    @PostMapping("/saveConsultationOrComplain")
    public R<?> saveConsultationOrComplain(@RequestBody Complain complain) {
        Map<String, String> result = new HashMap();
        try {
            String complainType = complain.getComplainType();
            if (StringUtils.isEmpty(complain.getId())) {
                LoginUser loginUser = TokenUtils.getMpLoginUser();
                complain.setUserId(loginUser.getLoginId());
                complain.setStatus("0");
                if (StringUtils.isNotBlank(complain.getComplainContent())) {
                    String complainContent = URLEncoder.encode(complain.getComplainContent(), "UTF-8");
                    complain.setComplainContent(complainContent);
                }

                if (complainType != null && "1".equals(complainType)) {
                    String no = getComplainNumber(loginUser.getPhone());
                    complain.setComplainNo(no);
                    complain.setComplainType("1");//0：咨询 1：投诉
                } else if (complainType != null && "0".equals(complainType)) {
                    String no = getConsultationNumber(loginUser.getPhone());
                    complain.setComplainNo(no);
                    complain.setComplainType("0");//0：咨询 1：投诉
                }
            } else {
                complain.setStatus("1");
            }
            complainService.save(complain);
            result.put("success", "true");
            result.put("message", "提交成功");
        } catch (Exception e) {
            result.put("success", "false");
            result.put("message", e.toString());
        }
        return R.appOk(result);
    }


    /**
     * @author llh
     * @date 2023/5/19
     * @return com.msk.common.core.domain.R<?>
     * @description 获取咨询投诉详情
     */
    @ApiOperation("获取咨询投诉详情")
    @PostMapping("/getViewDetails")
    public R<?> getViewDetails(@RequestBody Complain complain) {
        String id = complain.getId();
        if (id != null && id != "") {
            Complain result = complainService.get(id);
            String complainContent = result.getComplainContent();
            try {
                complainContent = URLDecoder.decode(complainContent, "UTF-8");
                result.setComplainContent(complainContent);
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
            return R.appOk(result);
        }
        return R.appFail(2001, "id不能为空");
    }

    /**
     * @author llh
     * @date 2023/5/19
     * @return java.lang.String
     * @description 获取投诉编号
     */
    public String getComplainNumber(String phone) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = sdf.format(new Date()).replace("-", "");
        return "TS" + nowDay + phone;
    }

    /**
     * @author llh
     * @date 2023/5/19
     * @return java.lang.String
     * @description 获取咨询编号
     */
    public String getConsultationNumber(String phone) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String nowDay = sdf.format(new Date()).replace("-", "");
        return "ZX" + nowDay + phone;
    }
}
